#import "InventoryDataSource.h"

@implementation InventoryDataSource

//===================================================================
// Init
//===================================================================
-(id) init
{
	[super init];
	
	ArticleItem *georog = [[ArticleItem alloc] init];
	ArticleItem *cable = [[ArticleItem alloc] init];
	
	[georog setName: @"GeoROG v6"];
	[georog setID: @"gre-v6"];
	[cable setName: @"GeoROG/6-Geodimeter 600 kabel"];
	[cable setID: @"007-001-010"];
	[cable setStock: [NSNumber numberWithInt:20]];
	[cable setPrice: [NSNumber numberWithInt:1280]];
	
	items = [[NSMutableArray alloc] init];
	[items addObject: georog];
	[items addObject: cable];
	[georog release];
	[cable release];
	
	return self;
}

//===================================================================
// Search
//===================================================================
-(IBAction)search: (id)sender
{
}

//===================================================================
// Fetch number of rows
//===================================================================
-(int) numberOfRowsInTableView: (NSTableView *) tableView
{
	return [items count];
}

//===================================================================
// Fetch cell
//===================================================================
-(id) tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row
{
	NSString *identifier = [tableColumn identifier];
	ArticleItem *item = [items objectAtIndex: row];
	
	return [item valueForKey: identifier];
}

//===================================================================
// Edit cell
//===================================================================
-(void) tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(int)row
{
	NSString *identifier = [tableColumn identifier];
	ArticleItem *item = [items objectAtIndex: row];
	
	[item takeValue:object forKey:identifier];
}

//===================================================================
// Sorting
//===================================================================
-(void) tableView:(NSTableView *)tableView didClickTableColumn:(NSTableColumn *)tableColumn
{
	NSString *identifier = [tableColumn identifier];
	if([identifier isEqualToString: @"name"])
	{
		[items sortUsingSelector:@selector(compareName:)];
	}
	else if([identifier isEqualToString:@"article_id"])
	{
		[items sortUsingSelector:@selector(compareID:)];
	}
	else if([identifier isEqualToString:@"stock"])
	{
		[items sortUsingSelector:@selector(compareStock:)];
	}
	else if([identifier isEqualToString:@"price"])
	{
		[items sortUsingSelector:@selector(comparePrice:)];
	}
	
	[table reloadData];
}

//===================================================================
// Add item
//===================================================================
- (IBAction)addItem: (id) sender
{
	ArticleItem *item = [[ArticleItem alloc] init];
	
	////////////////////////////////
	// Create the first changelog entry that tells
	// us that the item was added
	[item addChangeLogEntry:CHANGE_ADDED value:nil];
	
	[items insertObject:item atIndex:0];
	[item release];
	
	[table reloadData];
	[table selectRow:0 byExtendingSelection:NO];
}

//===================================================================
// Add order
//===================================================================
- (IBAction)addOrder:(id)sender
{
}

//===================================================================
// Save
//===================================================================
- (IBAction)save:(id)sender
{
	NSSavePanel *savePanel = [NSSavePanel savePanel];
	SEL sel = @selector(savePanelDidEnd:returnCode:contextInfo:);
	[savePanel
		beginSheetForDirectory:@"~/Documents"
		file:@"inventory.items"
		modalForWindow:[table window]
		modalDelegate:self
		didEndSelector:sel
		contextInfo:nil
	];
}

- (void)savePanelDidEnd:(NSSavePanel *)sheet returnCode:(int)returnCode contextInfo:(void *)context
{
	if(returnCode == NSOKButton)
	{
		[NSArchiver archiveRootObject:items toFile:[sheet filename]];
	}
}

//===================================================================
// Load
//===================================================================
- (IBAction)open:(id)sender
{
	NSOpenPanel *openPanel = [NSOpenPanel openPanel];
	SEL sel = @selector(openPanelDidEnd:returnCode:contextInfo:);
	[openPanel
		beginSheetForDirectory:@"~/Documents"
		file:@"inventory.items"
		types:nil
		modalForWindow:[table window]
		modalDelegate:self
		didEndSelector:sel
		contextInfo:nil
	];
}

- (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
	if(returnCode == NSOKButton)
	{
		NSMutableArray *array;
		array = [NSUnarchiver unarchiveObjectWithFile:[sheet filename]];
		[array retain];
		[items release];
		items = array;
		[table reloadData];
	}
}

@end
